<template>

  <div>
    <el-form ref="loginForm"
             :model="loginForm"
             :rules="rules"
             class="login-box"
             v-loading="loading"
             element-loading-text="正在登录..."
             element-loading-spinner="el-icon-loading"
             element-loading-background="rgba(0, 0, 0, 0.8)">
      <h3 class="login-title">欢迎登录</h3>
      <el-form-item prop="username">
        <el-input type="text" placeholder="请输入用户名" v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" placeholder="请输入密码" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input type="text" auto-complete="false" v-model="loginForm.code" placeholder="点击图片更换验证码"
                  style="margin-right: 5px;width: 250px;"></el-input>
        <img :src="captchaUrl" @click="updateCaptcha()">
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
        <el-button @click="resetForm('loginForm')" style="float: right">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>


export default {
  name: "Login",
  data() {
    return {
      captchaUrl: '/captcha?time=' + new Date(),
      loginForm: {
        username: 'admin',
        password: '123',
        code: ''
      },
      loading: false,
      checked: true,
      rules: {
        username: [{required: true, message: '用户名不为空', trigger: 'blur'}],
        password: [
          {required: true, message: '密码不为空', trigger: 'blur'},
          {min: 1, max: 6, message: '密码长度1-13位之间', trigger: 'blur'}
        ],
        code: [{required: true, message: '请输入验证码', trigger: 'blur'}]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.postRequest('/login', this.loginForm).then(resp => {
            if (resp) {
              this.loading = false;
              //存储用户token
              const tokenStr = resp.obj.tokenHead + resp.obj.token
              window.sessionStorage.setItem("tokenStr", tokenStr);
              //跳转首页
              let path = this.$route.query.redirect;
              console.log("redirect path = " + path)
              this.$router.replace((path === '/' || path == undefined) ? '/home' : path)
            }
          })
        } else {
          this.$message({message: '请输入必填字段', type: 'warning'});
          return false;
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    updateCaptcha() {
      this.captchaUrl = "/captcha?time=" + new Date();
    }
  }
}
</script>

<style>
.login-box {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background-color: #fff;
  border: #eeeeee;
  box-shadow: 0 0 25px #cac6c6;
}

.login-title {
  text-align: center;
  margin: 0 0 10px;
}

.el-form-item__content {
  display: flex;
  align-items: center;
}
</style>